package com.ztools.pc.controller.sys;

import com.ztools.common.model.pc.login.LoginToken;
import com.ztools.common.model.pc.sys.User;
import com.ztools.common.util.common.Result;
import com.ztools.common.util.common.annotateUnit.AccessLimit;
import com.ztools.common.util.pc.AccessConfig;
import com.ztools.pc.service.sys.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author:一口香菜
 * @Date: 2023/3/3  18:54
 * @Version 1.0
 */
@RestController
@RequestMapping("/sys/login")
public class LoginController {

    @Resource
    private UserService userService;

    @AccessLimit(key = "login", timeout = 5 * 1000)
    @AccessConfig(record = true)
    @RequestMapping("/login")
    public Result login(@RequestBody User user) {
        LoginToken token = userService.login(user);
        if (token != null) {
            return Result.success("登入成功").data(token);
        }
        return Result.error("登入失败");
    }

    @RequestMapping("/logout")
    public Result loginOut(String token) {
        userService.logout(token);
        return Result.success("退出成功");
    }
}
